<?php
/**
 * Talents come from diligence, and knowledge is gained by accumulation.
 *
 * @author: 晋<657306123@qq.com>
 * @date: 2019/8/5 16:31
 */

namespace xin\articlecollect\driver;

use QL\Dom\Elements;
use QL\QueryList;
use xin\articlecollect\AbsArticleCollect;

class Qqnews extends AbsArticleCollect{

	/**
	 * 内容解析
	 *
	 * @param string $data HTML内容
	 * @return array
	 */
	public function parse($data){
		$query = QueryList::html($data)->encoding('UTF-8', 'GB2312')->removeHead();
		$title = $query->find('h1')->text();

		$viewCount = 0;

		$content = $query->find('.content-article');
		$content->find('img')->map(function(Elements $item){
			$imgUrl = $item->attr('data-original-src');
			$item->removeAttr('data-original-src');
			if($imgUrl){
				$item->attr('src', $imgUrl);
			}
			return $item;
		});
		$content = $content->html();

		return [
			'title'       => $title,
			'content'     => $content,
			'view_count'  => $viewCount,
			'create_time' => time(),
		];
	}
}
